<!-- 模块说明 -->
<script setup lang='ts'>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Address } from '../../api';
import { useCustomersStore } from "./../../store"
const customerstore = useCustomersStore()
const route = useRoute();
const router = useRouter();
const chosenAddressId = ref('1');

const list = ref([])
const disabledList = [
    {
        id: '3',
        name: '王五',
        tel: '1320000000',
        address: '浙江省杭州市滨江区江南大道 15 号',
    },
];
onMounted(
    () => {
        zujian()
        showList()
    }
)
const zujian = () => {
    console.log(route.meta.fromComponent, "从哪里来的呢")
    // console.log(router,"谁跳到了地址列表")
}
const showList = async () => {
    let id: number = customerstore.id
    let res = await Address['getAddrList'](id)//这里的值是从pinia的本地存储里面拿到的

    // console.log(res.data, 'data')
    list.value = res.data.map((item: any) => {
        const newobj = { ...item }
        newobj.address = newobj.province + newobj.city + newobj.county + newobj.addressDetail
        return newobj
    })
    //把默认地址排在最前面
    const defaultItem = list.value.find((item: any) => item.isDefault == 1);
    if (defaultItem != null) {
        const remainingItems = list.value.filter((item: any) => item.isDefault == 0);
        list.value = [defaultItem, ...remainingItems];
    }
}

const onAdd = () => {
    console.log(route, "本组件有什么");
    console.log(router, "路由有什么");
    router.push('addr')
};

const onEdit = (item: any) => {
    router.push({ name: 'addr', params: { addrid: item.id } })
};
// const clickitem=(AddressListAddress:any)=>{
//     if(route.meta.fromComponent=="wode"){
//         router.push(`/wode/${AddressListAddress.id}`)
//     }
// }
</script>
<template>
    <div class="box">
        <van-address-list @edit="onEdit" v-model="chosenAddressId" :list="list" :disabled-list="disabledList"
            disabled-text="以下地址超出配送范围" default-tag-text="默认" @add="onAdd" />
    </div>
</template>

<style lang="scss" scoped>
.box {}
</style>